/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package code;

/**
 *
 * @author Catarina
 */

import java.util.*;
import java.io.*;

public class Rede implements Serializable{
    
    private TreeMap<String, User> users;
    
    public Rede(){
        users = new TreeMap<String, User>();
    }
    
    public Rede(Rede r){
        users = r.getUsers();
    }
    
    public Rede(TreeMap<String, User> r){
        TreeMap<String, User> novaRede = new TreeMap<String, User>();
        
        for(User us : r.values()) 
            novaRede.put(us.getNick(), us.clone());
    }
    
    
    public TreeMap<String, User> getUsers(){
        TreeMap<String, User> novo = new TreeMap<String, User>();
        for(User us : users.values()) novo.put(us.getNick(), us);
        return novo;
    }
    
    
    public void addUser(User us){
        User copy = us.clone();
        if(this.users.containsKey(us.getNick())) return;
        this.users.put(copy.getNick(), copy);
    }
    
    public void removeUser(User us){
        users.remove(us.getNick());
    }
    
    public User searchUsersNome(String nome){
        String key = "";
        User aux = new User();
        Iterator<String> it = users.keySet().iterator();
        boolean found = false;
        while(it.hasNext() && found == false){
            key = it.next();
            aux = users.get(key);
            if(aux.getNome().equals(nome))
                found = true;
        }
        if(found == false) return null;
        return aux;

    }
         
    public User searchUsersNick(String nick){
        String key = "";
        User aux = new User();
        Iterator<String> it = users.keySet().iterator();
        boolean found = false;
        while(it.hasNext() && found == false){
            key = it.next();
            aux = users.get(key);
            if(aux.getNick().equals(nick))
                found = true;
        }
        if(found == false) return null;
        return aux;
    }
    
    public void addPedidoGeral(User sender, User receiver){
        receiver.addPedido(sender);
    }
    
    public void addAmizade(User us1, User us2){
        us1.addAmigo(us2);
        us2.addAmigo(us1);
    } 
    
    /*@Override
    public String toString(){
        StringBuilder rede = new StringBuilder();
        rede.append("FOURSQUARE @UM\n\n");
        for(User us : users.values()){
            rede.append(us.toString());
            rede.append("-------------//-------------\n\n");
        }
        return rede.toString();
    }*/
    
    public String toStringFich() throws IOException{
        PrintWriter rede = new PrintWriter(new FileWriter("printBD.txt"));
        rede.println("FOURSQUARE @UM\n\n");
        for(User us : users.values()){
            rede.print(us.toString());
            rede.print("-------------//-------------\n\n");
        }
        rede.println();
        rede.close();
        return rede.toString();
    }
    
    
    public void toStringFichObjStream() throws IOException{
        
        ObjectOutputStream rede = new ObjectOutputStream(new FileOutputStream("printBDstream.txt"));
        try{
        rede.writeObject("FOURSQUARE @UM\n\n");
        for(User us : users.values()){
            rede.writeObject(us.toString());
            rede.writeObject("-------------//-------------\n\n");
        }
        
    }
        catch(IOException e) {System.out.println(e.getMessage());} 
        rede.flush();
        rede.close();
    }
    
    /*
    public void toStringObjStream(String fich) throws IOException{
        ObjectOutputStream rede = new ObjectOutputStream(new FileOutputStream("printBDstream.txt"));
        try{
            rede.writeObject(this);
        }
        catch(IOException e) {
            System.out.println(e.getMessage());
        }
        rede.flush();
        rede.close();
    }*/
    
}

